define(function(require, exports, module){
	require("resource/js/easyui/easyloader.js");
	var A = require("resource/js/component/ajax.js");
	var T = require("resource/js/component/treeutil.js");
	require("base/main/js/index.js");
	var Obj = window.loadI18n('base/user');
	function loadBanner(){
		var data = [{url:'',src:BASEPATH+'/resource/images/02.jpg'},
		            {url:'',src:BASEPATH+'/resource/images/01.jpg'},
		            //{url:'',src:BASEPATH+'/resource/images/02.jpg'}
		];
		$("#banner_list").html("");
		$.each(data,function (i,obj){
			var url ;
			if(obj!=undefined&&obj.hasOwnProperty("url")){
				url = obj.url;
			}
			if(!url){
				url="javascript:void(0);";
			}
			var str ;
			if(obj!=undefined&&obj.hasOwnProperty("src")){
				str="<a href=\""+url+"\"><img src=\""+obj.src+"\" height=\"290\"  width=\"100%\"/></a> ";
			}
			$("#banner_list_index").append("<li>"+(i+1)+"</li>")
			$("#banner_list").append(str);
		});
		initBaner();
	} 
	

	var t = n = 0, count; 
	var initBaner = function(){
		count=$("#banner_list a").length; 
		$("#banner_list a:not(:first-child)").hide(); 
		$("#banner").find("li").bind("click",function() {
			var i = $(this).text() - 1;
			n = i;
			if (i >= count) return; 
			$("#banner_list a").filter(":visible").fadeOut(500).parent().children().eq(i).fadeIn(1000); 
			$(this).css({"background":"#1E8CF9",'color':'#000'}).siblings().css({"background":"#e2e2e2",'color':'#fff'}); 
		}); 
		t = setInterval(function(){
			n = n >=(count - 1) ? 0 : ++n; 
			$("#banner").find("li").eq(n).trigger('click'); 
		}, 3000); 
		
		$("#banner").hover(function(){
				clearInterval(t)
			}, function(){
				t = setInterval(function(){
					n = n >=(count - 1) ? 0 : ++n; 
					$("#banner li").eq(n).trigger('click'); 
				}, 3000);
		});
		var frontHeight=$('.front-bd').height()-315;
		$('.panel-body_bd').height(frontHeight);
	}
	function bindEvent(){
		$('.awlays-use ul li img').click(function(){
			$(this).parents('li').remove();
		})
	}
	var createNotice = function(data){
		var ul = $("#message-content");
		ul.empty();
		if(data!=undefined){
			$.each(data.rows,function(i,item){
				var li = $("<li noticeId='"+item.noticeId+"'><span>"+item.title+"</span><div><span class='centerSpan'>"+item.createUserName+"</span><span>"+(function(time){if(time) return time.substring(0,10);else return "";})(item.endtime)+"</span></div></li>");
				li.appendTo(ul);
				li.click(function(){
					if(indexApp){				
						indexApp.exec("add",{name:""+Obj.get('base.common.success.Announcement')+"",url:BASEPATH+item.contentUrl,noframe:true});
						initNotice();
					}
				});
			});
		}
	}
	var createUsed = function(data){
		var ul = $("#use-top-content");
		ul.empty();
		$.each(data,function(i,item){
			var findSys = function(node){
				if(node.funType=='sys'){
					return node;
				}
				for(var i in menudata){
					it = menudata[i];
					if(it.id==node.pId){
						return findSys(it);
					}
				}
				return node;
			}
			
			ul.contextmenu(function(e){
				return false;
			});
			var li = $("<li style='background:url("+BASEPATH+(function(img){if(img){
				return img;
			}else return "/resource/images/menu/default.png";})(item.img)+") no-repeat center 10px' uid='"+item.id+"' usys='"+findSys(item).id+"' ulink='"+item.link+"'> <img src='../../../resource/images/gb_05.png'> <span>"+item.name+"</span> </li>");
			li.appendTo(ul);
			li.click(function(){
				if(indexApp) indexApp.exec("refrsh",{link:$(this).attr("ulink"),pid:$(this).attr("usys")});
			});
			/*li.mousedown(function(e){
		          if(3 == e.which){ 
		               $(this).draggable({  });
		               $(this).draggable("enable");
		          }
			});
			li.mouseup(function(){
				if(3 == e.which){ 
					$(this).draggable("disable");
				}
			})*/
			li.find("img").click(function(e){
				e.stopPropagation();
				A.init().success(function(code,msg,data){
					createUsed(data);
				}).post(BASEPATH+"/base/authoriza/user/removeUsedFunc.htm",{checkeds:$(this).parent().attr("uid")});
			});
		});
	}
	var getUseds = function(){
		var ul = $("#use-top-content");
		var arr = [];
		ul.find("li").each(function(){
			arr.push($(this).attr("uid"));
		});
		return arr;
	}
	var initNotice = function(){
		var noticeH = $(".sys-message").height()-28;
		var num = Math.ceil(noticeH/36)-1;
		A.init().success(function(code,msg,data){
			createNotice(data);
		}).post(BASEPATH+"/base/authoriza/notice/queryNoticeByPage.htm",{state:'0',endtime_start:new Date().format('yyyy-MM-dd hh:mm:ss'),isRead:false,pageNo:1,pageSize:num});
	}
	var initPage = function(){
		initNotice();
		
		$("#parent-more").click(function(){
			if(indexApp){
				indexApp.exec("add",Obj.get('base.common.success.notice'),$(this).attr("ulink"));
			}
		});
		A.init().success(function(code,msg,data){
			createUsed(data);
		}).post(BASEPATH+"/base/authoriza/user/getUsedFunc.htm");
		$("#usedtoadd").click(function(){
			var tree = new T.ajaxTree({
				eid:"treeselecter",
				localData:menudata,
				title:""+Obj.get('base.common.success.Commonfunction')+"",
				checks:getUseds(),
				save:function(data){
					var checkeds = "";
					$.each(data,function(i,item){
						if(i==data.length-1)
							checkeds += item.id;
						else 
							checkeds += item.id+",";
					});
					A.init().success(function(code,msg,data){
						createUsed(data);
					}).post(BASEPATH+"/base/authoriza/user/saveUsedFunc.htm",{checkeds:checkeds});
				}
			});
			var treeobj = null;
			tree.setting.callback.beforeCheck=function(treeId, treeNode) {
				if(treeNode.checked == false){
					//计算node节点下总共有多少个子节点
					function sumLength(node){
						if(node.isparent){
							var i = 0 ;
							for(var j in node.children){
								i = parseInt(sumLength(node.children[j]))+i;
							}
							return i;
						}else return 1;
					}
					
					var nodes = treeobj.getCheckedNodes(true);
					var len = nodes.length;
					//如果父节点选中  可忽略计算
					for(var i in nodes){
						if(nodes[i].isparent){
							len = len-1;
						}
					}
					if(len+sumLength(treeNode)>8){
						$.messager.show({
							title:""+Obj.get('base.common.success.Systemprompt')+"",
							msg:""+Obj.get('base.common.success.commonly')+"！",
							timeout:2000,
							showType:'slide'
						});
						return false;
					}else{
						return true;
					}
				}else{
					return true;
				}
			}
			tree.load(function(treeObj){
				treeobj = treeObj;
			});
			$("#treeselecter").dialog("open");
		});	
	}
	exports.init=function(){
		using(["dialog","textbox","parser","messager"],function(){
			initPage();
		});
		loadBanner();
		bindEvent();
		//loadContent();
		//loadMessage();
	}
});
